let fileObj = null;
var FormData = new FormData();
let tokenValue = localStorage.getItem('token');
let schoolYear = '',schoolSemester = '',schoolMouth = '';

window.onload = listFile;

function select(e,ev){
    $(e).find('.option').toggle();
    if (ev.localName === 'li'){
        $(`${e} span`).html($(ev).html());
        if (e === '#school-semester'){
            $('#school-semester span').html() === '上学期'?$('#school-mouth span').html('9月'):$('#school-mouth span').html('3月');
        }
        listFile();
    }
}

function closest(e,id) {
    if($(e.target).closest(id).length === 0){
        $(`${id} ul`).css('display','none');
    }
}

function mouth(){
    let mouthUp = [9,10,11,12,1,2];
    let mouthDown = [3,4,5,6,7];

    if ($('#school-semester span').html() === '上学期'){
        $('#school-mouth ul').html('');
        $.each(mouthUp,function () {
            $('#school-mouth ul').append(`<li>${this}月</li>`);
        });
    }

    if ($('#school-semester span').html() === '下学期'){
        $('#school-mouth ul').html('');
        $.each(mouthDown,function () {
            $('#school-mouth ul').append(`<li>${this}月</li>`);
        });
    }

}

$(document).on('click',e => {
    closest(e,'#school-year');
    closest(e,'#school-semester');
    closest(e,'#school-mouth');
    closest(e,'#switch-Grade-select');

}).on('click','#school-year',e => {
    select('#school-year',e.target);

}).on('click','#school-semester',e => {
    select('#school-semester',e.target);

}).on('click','#school-mouth',e => {
    mouth();
    select('#school-mouth',e.target);

}).on('click','#switch-Grade-select',e => {
    select('#switch-Grade-select',e.target);
    $(e.target).html() === '期末成绩'?window.location.href = '../pages/uploadFinalGradePage.html':false;
});


//上传文件事件
$('.file-updata-wrap').on('click','.forOpenFile',e => {
    //点击添加文件
    $(e.target).parent().find('input[type="file"]').click();

}).on('change','.openFile',function (e) {
    //上传文件
    fileObj = this.files[0];
    FormData.set('excel',fileObj);

    let warp = $(e.target).parent().find('section');
    $.each(warp,function () {
        $(this).css('display','block')
    });
    $(e.target).parent().css('border','1px solid #979797').find('.forOpenFile').css('display','none');
    $(e.target).parent().find('.file-message').text(fileObj.name);

    $(e.target).parent().find('.openFile').replaceWith('<input type="file" name="filename" class="openFile" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">');

}).on('click','.file-close',e => {
    //删除文件
    let warp = $(e.target).parent().find('section,div');
    $.each(warp,function () {
        $(this).css('display','none');
    });

    $(e.target).parent().css('border','1px dashed #979797').find('.forOpenFile').css('display','block');

    FormData.set('excel',null);
    fileObj = null;
});

$('.file-list-wrap').on('click','.file-close',e => {
    Sendajax('http://119.23.46.241:8080/admin/delxlsx','post',{
        id:$(e.target).parent().attr('idNum')
    },tokenValue);
    $(e.target).parent().remove();
});

$('.submit-button').on('click',() => {
    if (fileObj !== null){
        upData();
        FormData.set('time',schoolYear +'-'+ schoolSemester +'-'+ schoolMouth);
        FormData.set('gradeType','0');
        uploadGradeExcel();
        listFile()
    }
});

function uploadGradeExcel() {
    var uploadExcel = SendFileajaxPc('http://119.23.46.241:8080/admin/uploadGradeExcel','post', FormData,tokenValue);

    if (uploadExcel.code === 201){
        let warp = $('.file-updata-wrap').find('.file-wrap').find('section,div');
        $.each(warp,function () {
            $(this).css('display','none');
        });

        $('.file-updata-wrap').find('.file-wrap').css('border','1px dashed #979797').find('.forOpenFile').css('display','block');

        FormData.set('excel',null);
        fileObj = null;
    }else {
        $('.file-updata-wrap').find('.file-error').css({display:'block',background:'url("https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Delete@3x.png") no-repeat',backgroundSize:'cover'});
    }
}

function upData() {
    schoolMouth = $('#school-mouth span').html().replace('月','');
    let arrYear = ['第一学年','第二学年','第三学年','第四学年'];
    let arrSemester = ['上学期','下学期'];

    $.each(arrYear,function (item,value) {
        if (value === $('#school-year span').html()){
            schoolYear = item + 1;
        }
    });
    $.each(arrSemester,function (item,value) {
        if (value === $('#school-semester span').html()){
            schoolSemester = item + 1;
        }
    })
}

function addFileWrap(id,name) {
    let addSection = `<section class="file-wrap" idNum=${id}>
        <section class="file-icon"></section>
        <a href="javascript:" class="forOpenFile">暂无文件</a>
        <section class="file-message">${name}</section>
        <section class="file-close"></section>
        </section>`;

    $('.file-list-wrap').append(addSection);
}

function listFile() {
    upData();
    let listUpData = Sendajax('http://119.23.46.241:8080/admin/selxlsx','post',{
        time:schoolYear +'-'+ schoolSemester +'-'+ schoolMouth,
        gradeType:'0'
    },tokenValue).data;

    if (listUpData.length !== 0){
        $('.file-list-wrap').html('');
        $.each(listUpData,function () {
            addFileWrap(this.id,this.name)
        });
    }else {
        $('.file-list-wrap').html('');
    }
}